﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using Quellenverwaltung.Data.Source;
using Quellenverwaltung.Meta;

namespace Quellenverwaltung.Threads
{
    class ReadDOIProcess
    {
        public event EventHandler<DOIProcessArgs> ResultFound;
        private Word.Configuration configuration;

        public ReadDOIProcess()
        {
            configuration = Word.Configuration.getConfiguration();
        }

        /// <summary>
        /// Starts search in additional Thread.
        /// </summary>
        public void Start(String url)
        {
            ThreadStart start = new ThreadStart(delegate
            {
                DOIProcessArgs args = new DOIProcessArgs() { Result = this.Process(url) };
                this.ResultFound(this, args);
            });

            Thread t = new Thread(start);
            t.Start();
        }

        private ArticleInAPeriodical Process(String url)
        {
            try
            {
                ReadMetaDataFromDOISite reader = new ReadMetaDataFromDOISite();
                Dictionary<string, string> meta = reader.ReadMetaData(url);
                Data.Source.ArticleInAPeriodical journal = new BuildJournalSource().FromMetaData(meta);
                return journal;
            }
            catch
            {
                // Ignore
            }
            return null;
        }
    }

    /// <summary>
    /// Class to pass arguments between SearchProcess events.
    /// </summary>
    public class DOIProcessArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets the result.
        /// </summary>
        /// <value>
        /// The result.
        /// </value>
        public ArticleInAPeriodical Result { get; set; }
    }
}
